package com.winit.openapi.ece.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.ece.utils.EceLogisticInfoBuilder;
import com.winit.openapi.ece.utils.EceLogisticInfofieldValidator;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.vms.spi.EceLogisticsInfoService;
import com.winit.vms.spi.OrderService;
import com.winit.vms.spi.command.EceLogisticsInfoCommand;
import com.winit.vms.spi.command.TrackingNoCommand;
import com.winit.vms.spi.vo.EceLogisticsInfoVo;

/**
 * 进口订单物流信息交互入口
 * 
 * @version <pre>
 * Author   Version     Date        Changes
 * minghao.deng     1.0         2016年4月7日   Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/ece/order")
public class EceLogisticsInfoController extends BaseController {

    @Resource
    private EceLogisticsInfoService eceLogisticsInfoService;
    @Resource
    private OrderService            orderService;

    /**
     * 订单入库
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/storage")
    public String inBound(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            EceLogisticInfofieldValidator.validateEceInBound(json);
            EceLogisticsInfoCommand command = new EceLogisticsInfoCommand();
            EceLogisticsInfoVo vo = EceLogisticInfoBuilder.buildInBoundEceLogisticInfo(json);
            command.setEceLogisticsInfoVo(vo);
            command.setCtx(CommandContext.getContext());
            eceLogisticsInfoService.addLogisticsInfo(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        return SUCCESS;
    }

    /**
     * 订单出库
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/delivery")
    public String outBound(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 参数校验;
            EceLogisticInfofieldValidator.validateEceOutBound(json);
            EceLogisticsInfoCommand command = new EceLogisticsInfoCommand();
            EceLogisticsInfoVo vo = EceLogisticInfoBuilder.buildOutBoundEceLogisticInfo(json);
            command.setEceLogisticsInfoVo(vo);
            command.setCtx(CommandContext.getContext());
            eceLogisticsInfoService.addLogisticsInfo(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        return SUCCESS;
    }

    /**
     * 航班信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/flight")
    public String flight(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 参数校验;
            EceLogisticInfofieldValidator.validateEceFlight(json);
            EceLogisticsInfoCommand command = new EceLogisticsInfoCommand();
            EceLogisticsInfoVo vo = EceLogisticInfoBuilder.buildFlightEceLogisticInfo(json);
            command.setEceLogisticsInfoVo(vo);
            command.setCtx(CommandContext.getContext());
            eceLogisticsInfoService.addLogisticsInfo(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        return SUCCESS;
    }

    /**
     * 修改订单跟踪号
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/updateTrackingNo")
    public String updateTrackingNo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        TrackingNoCommand command = null;
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 参数校验;
            EceLogisticInfofieldValidator.validUpdateTrackingNoField(json);
            // 组装command;
            command = EceLogisticInfoBuilder.buildUpdateTrackingNoEceLogisticInfo(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        orderService.updateTrackingNo(command);

        return SUCCESS;
    }

    /**
     * 抵达国内口岸
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/arrivePort")
    public String arriveDomesticPort(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        EceLogisticsInfoVo vo = null;
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 参数校验;
            EceLogisticInfofieldValidator.validateEceArriveDomesticPort(json);
            // 组装vo;
            vo = EceLogisticInfoBuilder.buildArrivePortEceLogisticInfo(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        EceLogisticsInfoCommand command = new EceLogisticsInfoCommand();
        command.setCtx(CommandContext.getContext());
        command.setEceLogisticsInfoVo(vo);
        eceLogisticsInfoService.addLogisticsInfo(command);

        return SUCCESS;
    }

    /**
     * 国内清关
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/customsClearance")
    public String domesticCustomsClearance(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 参数校验;
            EceLogisticInfofieldValidator.validateEceCustomsClearance(json);
            EceLogisticsInfoCommand command = new EceLogisticsInfoCommand();
            EceLogisticsInfoVo vo = EceLogisticInfoBuilder.buildCustomsClearanceEceLogisticInfo(json);
            command.setEceLogisticsInfoVo(vo);
            command.setCtx(CommandContext.getContext());
            eceLogisticsInfoService.addLogisticsInfo(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        return SUCCESS;
    }

    /**
     * 转交国内快递
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/transport")
    public String transportToExpress(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 参数校验;
            EceLogisticInfofieldValidator.validateEceTransportToExpress(json);
            EceLogisticsInfoCommand command = new EceLogisticsInfoCommand();
            EceLogisticsInfoVo vo = EceLogisticInfoBuilder.buildTransportExpressEceLogisticInfo(json);
            command.setEceLogisticsInfoVo(vo);
            command.setCtx(CommandContext.getContext());
            eceLogisticsInfoService.addLogisticsInfo(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        return SUCCESS;
    }
}
